<template>
  <!-- 添加或修改业务对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
    <el-form ref="form" size="mini" :model="form" label-width="80px">

      <NumberItem @done="getNumberItem"/>
      <NameAndAddress @done="getNameAndAddress"/>
      <DateItem @done="getDateList" />
      <LegalItem @done="getLegalItem"/>
      <IsOtherLegal @done="getOtherLegal"/>
      <PatentAward @done="getPatentAward"/>
      <PatentValue @done="getPatentValue"/>

      <el-form-item style="position:relative;height:60px">
        <el-button size="medium" type="primary" @click="submitForm" style="background-color:#008ac0;position:absolute;right:10%">确 定</el-button>
        <el-button size="medium" type="primary" @click="cancel" style="background-color:#008ac0;position:absolute;right:22%">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>


<script>
  import {
    toRetrieval
  } from '@/api/retrieval'

  import NumberItem from '../retrieval/components/number.vue'
  import NameAndAddress from '../retrieval/components/nameAndAddress.vue'
  import DateItem from '../retrieval/components/dateItem.vue'
  import LegalItem from  '../retrieval/components/legalItem.vue'
  import IsOtherLegal from '../retrieval/components/isOtherLegal.vue'
  import PatentAward from '../retrieval/components/patentAward.vue'
  import PatentValue from '../retrieval/components/patentValue.vue'


  export default {
    name: "searchScreen",
    components: {
      NumberItem,
      NameAndAddress,
      DateItem,
      LegalItem,
      IsOtherLegal,
      PatentAward,
      PatentValue
    },
    data() {
      return {
        form: {},
        // 弹出层标题
        title: "筛选",
        // 是否显示弹出层
        open: false,
        number: [],
        nameAndAddress: [],
        dateList:[],
        legalList:[],
        otherLegalList:[],
        patentAwardList:[],
        patentValueList:[]
        
      };
    },
    methods: {
      getNumberItem(data) {
        this.number = data
      },
      getNameAndAddress(data) {
        this.nameAndAddress = data
      },
      getDateList(data){
        this.dateList=data
      },
      getLegalItem(data){
        this.legalList=data
      },
      getOtherLegal(data){
        this.otherLegalList=data
      },
      getPatentAward(data){
        this.patentAwardList=data
      },
      getPatentValue(data){
        this.patentValueList=data
      },

      init() {
        this.open = true;
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      /** 提交按钮 */
      submitForm: function () {
        let queryConditions
        if (JSON.stringify(this.dateItem) === '{}') {
          queryConditions = [
              ...this.number, 
              ...this.nameAndAddress,
              ...this.dateList,
              ...this.legalList,
              ...this.otherLegalList,
              ...this.patentAwardList,
              ...this.patentValueList
              
              ]

          if (queryConditions.filter(item => {
            return item.value
          }).length == 0) {
            this.$message({
              message: '检索条件不能为空',
              type: 'warning'
            })
            return
          }

        } else {
          queryConditions = [
              ...this.number, 
              ...this.nameAndAddress,
              ...this.dateList,
              ...this.legalList,
              ...this.otherLegalList,
              ...this.patentAwardList,
              ...this.patentValueList
              ]
        }

        const queryColumn = []
        const pageIndex = 1
        const pageSize = 20

        const data = {
          queryConditions: queryConditions,
          queryColumn,
          pageIndex,
          pageSize
        }

        let searchData = JSON.parse(localStorage.getItem("searchData"))
        if (searchData) {
          let newConditions = [...searchData.queryConditions, ...data.queryConditions]
          data.queryConditions = newConditions
        }

        localStorage.setItem("searchData", JSON.stringify(data))

        toRetrieval(data).then(res => {
          this.$emit('submit-init', res.data);
        })

        this.open = false
      }
    }
  };
</script>
